package com.yoooya.base.admin.config;

import com.yoooya.base.admin.interceptor.MonitorInterceptor;
import com.yoooya.base.admin.interceptor.PermissionInterceptor;
import com.yoooya.base.config.*;
import com.yoooya.base.converter.DateConverter;
import org.springframework.context.annotation.*;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.HandlerAdapter;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;
import org.springframework.web.servlet.handler.SimpleServletHandlerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import java.nio.charset.StandardCharsets;
import java.util.List;

//CacheConfig.class,

@Configuration
//@EnableWebMvc
@EnableAspectJAutoProxy
@ComponentScan("com.yoooya.base")
@Import({ EnvironmentConfig.class, DalConfig.class, MyBatisConfig.class,
		 SchedulingConfig.class,SpringShiroConfig.class })
public class WebConfig extends WebMvcConfigurationSupport {

	@Override
	protected void extendMessageConverters(
			List<HttpMessageConverter<?>> converters) {
		for (HttpMessageConverter<?> e : converters) {
			if (e instanceof StringHttpMessageConverter) {
				converters.remove(e);
				StringHttpMessageConverter bean = new StringHttpMessageConverter(
						StandardCharsets.UTF_8);
				bean.setWriteAcceptCharset(false);
				converters.add(0, bean);
				break;
			}
		}
		/*
		 * for(HttpMessageConverter<?> e : converters) { if(e instanceof
		 * MappingJackson2HttpMessageConverter) {
		 * MappingJackson2HttpMessageConverter bean =
		 * (MappingJackson2HttpMessageConverter) e; bean.setObjectMapper(new
		 * JacksonObjectMapper()); List<MediaType> supportedMediaTypes = new
		 * ArrayList<>(); supportedMediaTypes.add(new MediaType("application",
		 * "json", StandardCharsets.UTF_8)); supportedMediaTypes.add(new
		 * MediaType("text", "json", StandardCharsets.UTF_8));
		 * supportedMediaTypes.add(new MediaType("text", "html",
		 * StandardCharsets.UTF_8)); supportedMediaTypes.add(new
		 * MediaType("text", "plain", StandardCharsets.UTF_8));
		 * bean.setSupportedMediaTypes(supportedMediaTypes); break; } }
		 */
	}

	@Override
	protected void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/themes/**", "/*.ico").addResourceLocations("/themes/");
		registry.addResourceHandler("/static/**").addResourceLocations("/static/");

		registry.addResourceHandler("/layuiAdmin.std-v1.2.1/**").addResourceLocations("/layuiAdmin.std-v1.2.1/");
		registry.addResourceHandler("/layuiAdmin/**").addResourceLocations("/layuiAdmin/");
	}

	@Bean
	@Override
	public HandlerMapping resourceHandlerMapping() {
		return super.resourceHandlerMapping();
	}

	@Override
	protected void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(new MonitorInterceptor());
		registry.addInterceptor(new PermissionInterceptor());
	}

	@Bean
	@Override
	public RequestMappingHandlerMapping requestMappingHandlerMapping() {
		return super.requestMappingHandlerMapping();
	}

	@Bean
	public HandlerAdapter servletHandlerAdapter() {
		return new SimpleServletHandlerAdapter();
	}

	@Bean
	public ViewResolver viewResolver() {
		InternalResourceViewResolver bean = new InternalResourceViewResolver();
		bean.setPrefix("/layuiAdmin/src/views/");
		bean.setSuffix(".html");

		return bean;
	}

	@Bean
	public SimpleMappingExceptionResolver exceptionResolver() {
		SimpleMappingExceptionResolver bean = new SimpleMappingExceptionResolver();
		bean.setDefaultErrorView("/error/500");
		bean.setDefaultStatusCode(500);
		return bean;
	}

	@Bean
	public CommonsMultipartResolver multipartResolver() {
		CommonsMultipartResolver bean = new CommonsMultipartResolver();
		bean.setDefaultEncoding("UTF-8");
		return bean;
	}

	/**
	 * 描述 : <注册通用属性编辑器>. <br>
	 * <p>
	 * <这里只增加了字符串转日期和字符串两边去空格的处理>
	 * </p>
	 * 
	 * @return
	 */
	@Override
	protected ConfigurableWebBindingInitializer getConfigurableWebBindingInitializer() {
		ConfigurableWebBindingInitializer initializer = super
				.getConfigurableWebBindingInitializer();
		initializer.setPropertyEditorRegistrar(new DateConverter());
		return initializer;
	}
}
